<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>websocket</title>
</head>
<body>
<h1>echo test</h1>
<input id="sendText" type="text" />
<button id="sendBtn">发送</button>
<div id="recv"></div>
</body>
<script type="text/javascript">
    var websocket=new WebSocket("ws://localhost:9000");
    function showMessage(str){
        var div=document.createElement("div");
        div.innerHTML=str;
        document.body.appendChild(div);
    }
    websocket.onopen=function(){
        console.log("websocket open");
        document.getElementById("sendBtn").onclick=function(){
            var txt=document.getElementById("sendText").value;
            if(txt){
                websocket.send(txt);
            }

        }
    }
    websocket.onclose=function(){
        console.log("websocket close");

    }
    websocket.onmessage=function(e){
        console.log(e.data);
        showMessage(e.data);
    }

</script>

</html>
